package com.xxmassdeveloper.mpchartexample;

import android.Manifest;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import com.permissionx.guolindev.PermissionX;
import com.permissionx.guolindev.callback.RequestCallback;

import java.util.List;

public class TestFragmentActivity extends AppCompatActivity {
    public interface  IListener {
        void onFinish(View view);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test_fragment);
        PermissionX.init(this).permissions(Manifest.permission.WRITE_EXTERNAL_STORAGE).request(new RequestCallback() {
            @Override
            public void onResult(boolean allGranted, List<String> grantedList, List<String> deniedList) {
                if (allGranted) {
                    Toast.makeText(TestFragmentActivity.this, "All permissions are granted", Toast.LENGTH_LONG).show();
                    createFragment();
                } else {
                    Toast.makeText(TestFragmentActivity.this, "These permissions are denied: $deniedList", Toast.LENGTH_LONG).show();
                }

            }
        });
    }

    private void createFragment() {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        MyFragment fragment = new MyFragment("啦啦啦啦啦啦啦", new IListener() {
            @Override
            public void onFinish(View view) {
                CaptureUtil.saveImg(convertViewToBitmap(view));
            }
        });
        ft.add(R.id.container_clone, fragment, "my");
        ft.commitAllowingStateLoss();
    }

    //给View截图，代码应该没有问题
    public static Bitmap convertViewToBitmap(View view) {
        view.measure(View.MeasureSpec.makeMeasureSpec(1280, View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(800, View.MeasureSpec.EXACTLY));
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.buildDrawingCache();
        return view.getDrawingCache();
    }
}